package com.blueocean_health.care.management.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.util.JsoupUtils;

/**
 * 护工调拨记录
 *
 *
 * @Title: AllotRecordController.java
 * @Package com.blueocean_health.care.management.controller
 * @author luxue
 * @date 2018年8月27日
 * @version V1.0.8
 */
@RestController
@RequestMapping("web/nurse/allot")
public class AllotRecordController {
	@Value("${nurse.service.address}")
	private String nurseServiceAddress = "http://127.0.0.1:11005";

	/**
	 * 调出
	 * 
	 * @Title: callout
	 * @param allotRecordDto
	 * @return SysResult
	 * @version V1.0.8
	 * @throws IOException
	 */
	@PostMapping(value = "/callout", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public SysResult callout(@RequestBody Map<String, String> map) throws IOException {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("calloutTeamId", user.getHospitalId());
		return JsoupUtils.postConnectToSysResult(nurseServiceAddress + "/nurse/allot/callout", map);
	}

	/**
	 * 批量调出
	 * 
	 * @Title: batchCallout
	 * @param allotRecordDto
	 * @return SysResult
	 * @version V1.0.8
	 * @throws IOException
	 */
	@PostMapping(value = "/callout-batch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public SysResult batchCallout(@RequestBody Map<String, Object> map) throws IOException {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("calloutTeamId", user.getHospitalId());
		return JsoupUtils.postConnectToSysResult(nurseServiceAddress + "/nurse/allot/callout-batch", map);
	}

	/**
	 * 查询当前项目，待调入人数
	 * 
	 * @Title: awaitCallInCount
	 * @return SysResult
	 * @version V1.0.8
	 * @throws IOException
	 */
	@PostMapping(value = "/callin-await/count", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public SysResult awaitCallInCount() throws IOException {
		Map<String, String> map = new HashMap<>();
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("teamId", user.getHospitalId());
		return JsoupUtils.postConnectToSysResult(nurseServiceAddress + "/nurse/allot/callin-await/count", map);
	}

	/**
	 * 待调入列表
	 * 
	 * @Title: awaitCallInList
	 * @return SysResult
	 * @version V1.0.8
	 * @throws IOException
	 */
	@PostMapping(value = "/callin-await/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public SysResult awaitCallInList(@RequestBody Map<String, String> map) throws IOException {
		if (StringUtils.isEmpty(map) || map.size() <= 0 || !map.containsKey("page") || !map.containsKey("pageSize")) {
			return SysResult.error("参数错误");
		}
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("teamId", user.getHospitalId());
		return JsoupUtils.postConnectToSysResult(nurseServiceAddress + "/nurse/allot/callin-await/list", map);
	}

	/**
	 * 调入
	 * 
	 * @Title: awaitCallInList
	 * @return SysResult
	 * @version V1.0.8
	 * @throws IOException
	 */
	@PostMapping(value = "/callin", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public SysResult callin(@RequestBody Map<String, String> map) throws IOException {
		if (StringUtils.isEmpty(map) || map.size() <= 0 || !map.containsKey("id") || !map.containsKey("callinDate")) {
			return SysResult.error("参数错误");
		}
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("teamId", user.getHospitalId());
		return JsoupUtils.postConnectToSysResult(nurseServiceAddress + "/nurse/allot/callin", map);
	}

	/**
	 * 批量调入
	 * 
	 * @Title: awaitCallInList
	 * @return SysResult
	 * @version V1.0.8
	 * @throws IOException
	 */
	@PostMapping(value = "/callin-batch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public SysResult batchCallin(@RequestBody Map<String, Object> map) throws IOException {
		if (StringUtils.isEmpty(map) || map.size() <= 0 || !map.containsKey("ids") || !map.containsKey("callinDate")) {
			return SysResult.error("参数错误");
		}
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("teamId", user.getHospitalId());
		return JsoupUtils.postConnectToSysResult(nurseServiceAddress + "/nurse/allot/callin-batch", map);
	}

	/**
	 * 单个护工的调拨记录
	 * 
	 * @Title: awaitCallInList
	 * @return SysResult
	 * @version V1.0.8
	 * @throws IOException
	 */
	@PostMapping(value = "/query", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public SysResult queryAllotRecord(@RequestBody Map<String, String> map) throws IOException {
		if (StringUtils.isEmpty(map) || map.size() <= 0 || !map.containsKey("workerId") || !map.containsKey("page")
				|| !map.containsKey("pageSize")) {
			return SysResult.error("参数错误");
		}
		return JsoupUtils.postConnectToSysResult(nurseServiceAddress + "/nurse/allot/query", map);
	}

}
